﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using CommonHelp;
using Core.Unity;
using Microsoft.Practices.Unity; 
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Services; 
using ViewModels;

namespace Commands {
    /// <summary>
    /// 地震信息
    /// </summary>
    public class QueryEarthquakeCommand : CommandBase {
        private IXContainer _hook;
        private IDisplayTable _IDisplayTable;
        WebViewModel _WebViewModel;
        CommandAdapter _SaveDataCommand;

        public override void OnCreate(object hook) {
            base.OnCreate(hook);
            _hook = hook as IXContainer;

            _IDisplayTable = _hook.Resolve<IDisplayTable>();
            _WebViewModel = _hook.Resolve<WebViewModel>();

            CommandManagerWpf cmds = _hook.Resolve<CommandManagerWpf>();
            _SaveDataCommand = cmds["savedatacommand"] as CommandAdapter;
        }

        public override void OnClick(object parameter) {
            try {
                base.OnClick(parameter);
                HttpHelper http = new HttpHelper("http://www.ceic.ac.cn/ajax/speedsearch?num=6");
                http.GetAsync(AfterGetData);
                this.IsEnabled = false;
                _SaveDataCommand.IsEnabled = false;
            }
            catch (Exception ex) {
                CommonHelp.LogHelper.Error(ex);
            }
        }

        public void AfterGetData(string responseString) {
            //try {
            //    string jsonStr = responseString.Trim(new char[] { ' ', '(', ')' });
            //    JObject jsObj = JObject.Parse(jsonStr);
            //    JToken shuju = jsObj["shuju"];
            //    List<JToken> nodes = shuju.ToList();

            //    List<EarthquakeInfo> infos = new List<EarthquakeInfo>();
            //    nodes.ForEach(a => {
            //        EarthquakeInfo info = JsonConvert.DeserializeObject<EarthquakeInfo>(a.ToString());
            //        infos.Add(info);
            //    });

            //    _IDisplayTable.DisplayData = infos.ToArray();
            //    _IDisplayTable.AddRowVisitor = new AddEarthquakeInfo(_hook.Resolve<DbHelp.DbHelper>());
            //    _hook.Resolve<ITextHint>().AddText(DateTime.Now.ToString() + " 获取到 " + infos.Count.ToString() + " 条 地震 数据。");
            //}
            //catch (Exception ex) {
            //    CommonHelp.LogHelper.Error(ex);
            //    throw ex;
            //}
            //finally {
            //    this.IsEnabled = true;
            //    _SaveDataCommand.IsEnabled = true;
            //    //_WebViewModel.Url = "http://www.ceic.ac.cn/onmap/id:5";
            //}

            ////----------
            //try {
            //    using (WebDataDb db = new WebDataDb()) {
            //        string jsonStr = responseString.Trim(new char[] { ' ', '(', ')' });
            //        JObject jsObj = JObject.Parse(jsonStr);
            //        JToken shuju = jsObj["shuju"];
            //        List<JToken> nodes = shuju.ToList();

            //        List<WEBDATA_EARTHQUAKE> infos = new List<WEBDATA_EARTHQUAKE>();
            //        nodes.ForEach(a => {
            //            WEBDATA_EARTHQUAKE info = JsonConvert.DeserializeObject<WEBDATA_EARTHQUAKE>(a.ToString());
            //            infos.Add(info);
            //        });

            //        _IDisplayTable.DisplayData = infos.ToArray();
            //        _IDisplayTable.AddRowVisitor = new AddEarthquakeInfo(_hook.Resolve<DbHelp.DbHelper>());
            //        _hook.Resolve<ITextHint>().AddText(DateTime.Now.ToString() + " 获取到 " + infos.Count.ToString() + " 条 地震 数据。");
            //    }
            //}
            //catch (Exception ex) {

            //    throw;
            //}
        }

    }
}
